<template>
  <div class="login-container">
    <el-form
      :model="ruleForm2"
      :rules="rules2"
      status-icon
      ref="ruleForm2"
      label-position="left"
      label-width="0px"
      class="demo-ruleForm login-page"
    >
      <h2
        class="title"
        style="margin-bottom: 3.5%"
      >
        登录
      </h2>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="ruleForm2.account"
          auto-complete="off"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="ruleForm2.password"
          auto-complete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class="rememberme">记住密码</el-checkbox>
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          class="login"
          style="width: 176px"
          @click="handleSubmit"
          :loading="logining"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { adminlogin } from "@/api/index";
export default {
  data() {
    return {
      logining:false,
      ruleForm2: {
        account: "",
        password: "",
      },
      rules2: {
        account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      checked: false,
    };
  },
  methods: {
    login() {},
    adminlogin() {
      if (this.logining) {
        return false;
      }
      this.logining = true
      adminlogin(this.ruleForm2)
        .then((res) => {
          
          this.$message.success("登陆成功");
          this.logining = false;
          localStorage.setItem("user", res.data.account);
          localStorage.setItem('token',res.data.token)
          this.$router.push({ path: "/layout/index" });
        })
        .catch((res) => {
          this.logining = false
        });
    },
    handleSubmit(event) {
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          this.adminlogin();
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  .login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 292px auto;
    width: 348px;

    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0px 0px 5px rgb(0 0 0 / 35%);
    .el-input__inner {
      width: 292px;
      height: 30px;
    }
    label.el-checkbox.rememberme {
      margin: 0px 0px 15px;
      text-align: left;
    }
    .el-checkbox__label {
      padding-left: 5px;
    }

    label.el-checkbox.rememberme {
      margin-left: 27px;
    }
    .el-input {
      text-align: center;
    }
    .title {
      text-align: center;
      font-size: 18px;
      margin-top: 37px;
    }
    .login {
      height: 35px;
      background-color: #333333;
      border: none;
    }
    .el-button--primary:hover {
      background-color: #333333;
      border: none;
    }
    .el-form-item__content {
      text-align: center;
    }
  }
}
</style>